package me.zhengjie.inter.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.vo.Kc08Vo;
import me.zhengjie.business.domain.vo.PersonFeeVo;
import me.zhengjie.business.service.dto.OrderQueryCriteria;
import me.zhengjie.inter.service.MpJfFcService;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.time.LocalDate;
import java.util.*;

/**
 * @author lyl
 * @since 2023/11/20
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MpJfFcServiceImpl implements MpJfFcService {

    @PersistenceContext
    private EntityManager em;

    @Override
    public Map<String, Object> getPersonNoFee(OrderQueryCriteria criteria) {
        String ym = DateTimeUtil.getDateTime("yyyyMM");

        String tempSql =  "select t.aac001," +
                "       t.aac003," +
                "       t.aac002," +
                "       t.accrym_begn," +
                "       t.accrym_end," +
                "       t.insutype," +
                "       t.clct_clag" +
                "  from VIEW_PERSON_FEE t" +
                " where 1 = 1 and  t.clct_clag = 1 and t.insutype in ('310', '390', '411')"+
                " and t.accrym_begn <= :ym and t.accrym_end >= :ym " +
                " and t.aac002=:sfzh ";

        Query query = em.createNativeQuery(tempSql);
        query.setParameter("sfzh", criteria.getSfzh());
        query.setParameter("ym", ym);

        List result = query.getResultList();
        Map<String,Object> map = new HashMap<>();
        map.put("size", result.size());

        return map;
    }

    @Override
    public Map<String, Object> getPersonNoFc(OrderQueryCriteria criteria) {
        String tempSql = "select " +
                "       t.psn_no, " +
                "       t.psn_name, " +
                "       t.psn_certno, " +
                "       t.aac001, " +
                "       t.insutype, " +
                "       t.insutype_name, " +
                "       t.emp_name, " +
                "       t.insu_admdvs, " +
                "       t.paus_type, " +
                "       t.paus_type_name, " +
                "       t.chg_rea_dscr, " +
                "       t.begndate, " +
                "       t.enddate " +
                "   from KC08 t" +
                "   where ((t.begndate <= sysdate and t.enddate >= sysdate) or" +
                "       t.enddate is null)"+
                "       and t.psn_certno=:sfzh ";



        Query query = em.createNativeQuery(tempSql);
        if(!StringUtils.isEmpty(criteria.getSfzh())) {
            query.setParameter("sfzh", criteria.getSfzh());
        }

        List result = query.getResultList();
        Map<String,Object> map = new HashMap<>();
        map.put("size", result.size());

        return map;
    }
}
